﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

// JW Video Player
[assembly: WebResource("UmbracoVideoModule.Resources.swfobject.js", "application/x-javascript")]
[assembly: WebResource("UmbracoVideoModule.Resources.SwfPlayer.player.swf", "application/octet-stream")]
[assembly: WebResource("UmbracoVideoModule.Resources.SwfPlayer.yt.swf", "application/octet-stream")]

namespace UmbracoVideoModule
{

    public enum VideoControlBarPosition
    {
        Bottom = 0,
        Over = 1
    }

    [DefaultProperty("URL")]
    [ToolboxData("<{0}:Video runat=server></{0}:Video>")]
    public class Video : WebControl, INamingContainer
    {

        private string strYT, strVP;

        public Video()
        {
            this.AllowFullScreen = true;
            this.AutoStart = false;
            this.ControlBarPosition = VideoControlBarPosition.Bottom;

            //Default Size
            this.Width = Unit.Pixel(470);
            this.Height = Unit.Pixel(320);
            this.BackColor = System.Drawing.Color.White;
        }

        [Bindable(true), DefaultValue(""), Category("Urls")]
        public string URL
        {
            get { return (String)ViewState["URL"] ?? ""; }
            set { ViewState["URL"] = value; }
        }

        [Bindable(true), DefaultValue(""), Category("Urls")]
        public string PreviewImageUrl
        {
            get { return (String)ViewState["PreviewImageUrl"] ?? ""; }
            set { ViewState["PreviewImageUrl"] = value; }
        }

        [Bindable(true), DefaultValue(""), Category("Urls")]
        public string LogoImageUrl
        {
            get { return (String)ViewState["LogoImageUrl"] ?? ""; }
            set { ViewState["LogoImageUrl"] = value; }
        }

        [Bindable(true), DefaultValue(true)]
        public bool AllowFullScreen
        {
            get { return (bool)ViewState["AllowFullScreen"]; }
            set { ViewState["AllowFullScreen"] = value; }
        }

        [Bindable(true), DefaultValue(VideoControlBarPosition.Bottom), Category("Layout")]
        public VideoControlBarPosition ControlBarPosition
        {
            get
            {
                String s = (String)ViewState["ControlBarPosition"];
                if (s == null)
                    return VideoControlBarPosition.Bottom;
                else
                    return (s == "Bottom" ? VideoControlBarPosition.Bottom : VideoControlBarPosition.Over);
            }
            set
            {
                ViewState["ControlBarPosition"] = value.ToString();
            }
        }

        [Bindable(true), DefaultValue(false), Category("Behavior")]
        public bool AutoStart
        {
            get { return (bool)ViewState["AutoStart"]; }
            set { ViewState["AutoStart"] = value; }
        }

        private void RegisterClientScript()
        {
            ClientScriptManager csm = this.Page.ClientScript;
            csm.RegisterClientScriptResource(this.GetType(), "UmbracoVideoModule.Resources.swfobject.js");

            strYT = csm.GetWebResourceUrl(this.GetType(), "UmbracoVideoModule.Resources.SwfPlayer.yt.swf");
            strVP = csm.GetWebResourceUrl(this.GetType(), "UmbracoVideoModule.Resources.SwfPlayer.player.swf");
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            this.RegisterClientScript();
        }

        protected override void RenderContents(HtmlTextWriter output)
        {

            string strPlayer;
            if (this.URL.Contains("www.youtube.com") == true)
            {
                strPlayer = strYT;
            }
            else
            {
                strPlayer = strVP;
            }

            output.Write("<div id=\"" + this.ClientID + "_container\"><a href=\"http://www.macromedia.com/go/getflashplayer\">Get the Flash Player</a> to see this player.</div>\r\n" +
                        "<script type=\"text/javascript\">\r\n" +
                        "   var s1 = new SWFObject(\"" + strPlayer + "\",\"ply\",\"" + this.Width.ToString() + "\",\"" + this.Height.ToString() + "\",\"9\",\"#FFFFFF\");\r\n" +
                        "   s1.addParam(\"allowfullscreen\", \"" + this.AllowFullScreen.ToString().ToLower() + "\");\r\n" +
                        "   s1.addParam(\"allowscriptaccess\", \"always\");\r\n" +
                        "   s1.addParam(\"flashvars\", \"" + this.VideoParamsContructor() + "\");\r\n" +
                        "   s1.write(\"" + this.ClientID + "_container\");\r\n" +
                        "</script>");
        }

        private string VideoParamsContructor()
        {
            string str = "";

            if (this.URL.Length != 0)
                str += "&file=" + this.URL;

            if (this.PreviewImageUrl.Length != 0)
                str += "&image=" + this.PreviewImageUrl;

            if (this.ControlBarPosition == VideoControlBarPosition.Over)
                str += "&controlbar=over";

            if (this.LogoImageUrl.Length != 0)
                str += "&logo=" + this.LogoImageUrl;

            if (this.BackColor != null)
            {
                System.Drawing.Color c = this.BackColor;
                str += "&backcolor=" + System.Drawing.ColorTranslator.ToHtml(System.Drawing.Color.FromArgb(c.A, c.R, c.G, c.B));
            }

            str += "&autostart=" + this.AutoStart.ToString().ToLower();

            return str;
        }

    }
}
